package com.zzptc.myk.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ShareActionProvider;
import android.widget.Toast;

import com.zzptc.myk.myapplication.Bean.User;

import org.litepal.LitePal;

import java.util.List;

public class LoginActivity extends AppCompatActivity {
    EditText usndeit,psdedit;
    Button registerbtn,loginbtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        User user=new User(0,"admin","admin",2);
        user.save();
        initView();
        setListener();
    }

    private void setListener() {
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usn=usndeit.getText().toString().trim();
                String psd=psdedit.getText().toString().trim();
                User user=confirmUser(usn,psd);
                if(user==null){
                    showToast("账号密码错误");
                }else {
                    SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
                    SharedPreferences.Editor editor=sp.edit();
                    editor.putInt("userid",user.getId());
                    editor.putString("username",user.getName());
                    editor.putString("password",user.getPassword());
                    editor.apply();
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });
        registerbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usn=usndeit.getText().toString().trim();
                String psd=psdedit.getText().toString().trim();
                User user=new User(0,usn,psd);
                if (user.save()){
                    showToast("注册成功");
                }else {
                    showToast("该用户已存在，请登录");
                }
            }
        });
    }

    private void showToast(String str){
        Toast.makeText(LoginActivity.this,str,Toast.LENGTH_SHORT).show();
    }

    private User confirmUser (String usn, String psd){
        List<User> userList= LitePal.findAll(User.class);
        for (User u:userList){
            if (usn.equals(u.getName())&&psd.equals(u.getPassword())){//判断账号密码正确
                return u;//正确返回
            }
        }
        return null;//错误返回
    }

    //实例化
    private void initView() {
        usndeit=findViewById(R.id.usnedit);
        psdedit=findViewById(R.id.psdedit);
        registerbtn=findViewById(R.id.registerbtn);
        loginbtn=findViewById(R.id.loginbtn);
    }
}
